import requests
from lxml import etree
import string
from urllib.parse import quote
url = 'https://www.weibotop.cn/'
base_url = 'https://www.weibotop.cn/apis/getrankhistory.php?name='
headers = {
        "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"
        }
res = requests.get(url,headers=headers)
if res.status_code == 200:
    res.encoding = 'utf-8'
    tree = etree.HTML(res.text)
    entry = {}
    for i in range(1, 2):
        entry['title'] = tree.xpath('//*[@id="container"]/div[%s]/p[1]/text()'%i)
        keyword = str(entry['title'])[4:-2]
        print(keyword)
            # str(entry['title'])[4:-2]
        # xhr = base_url+keyword
        # xhr = quote(xhr, safe=string.printable)
        # detail = requests.get(xhr,headers=headers)
        # if detail.status_code == 200:
        #     json = detail.json()
        #     # print(json)
        #     entry['rank_no'] = json[1][-1]
        #     entry['hot_number'] = json[2][-1]

        detail_url = quote('https://m.weibo.cn/api/container/getIndex?containerid=100103type%3D1%26q%3D'+keyword+'&page_type=searchall', string.printable)
        print(detail_url)
        detail = requests.get(detail_url,headers=headers)
        if detail.status_code == 200:
            json = detail.json()
            mblog = json.get('data').get('cards')[0].get('mblog')
            user = json.get('data').get('cards')[0].get('mblog').get('user')
            longText = json.get('data').get('cards')[0].get('mblog').get('longText')
            print("发布人：", user['screen_name'])
            print("认证信息：",user['verified_reason'])
            print("个人描述：",user['description'])
            print("粉丝数:",user['followers_count'])

            print("----------微博内容-------")
            print(longText['longTextContent'])

            print("转发：",mblog['reposts_count'])
            print("评论数：",mblog['comments_count'])
            print("点赞数：",mblog['attitudes_count'])

            topic = longText['url_objects'][0]

            print("超话：",topic['url_ori'])
            print("搜索热度：",topic['search_topic_count'])
            print("话题阅读数量：", topic['search_topic_read_count'])

            # KeyError: 'object'
            topic_detail = topic['object']['object']
            print("超话名称：",topic_detail['display_name'])
            print("超话对应网址：", topic_detail['url'])
            print("超话简介：", topic_detail['summary'])
            print("创建时间", topic_detail['create_at'])
            print("话题类别",topic_detail['category'])
            print("热搜排名", topic_detail['ever_in_hotsearch'])
            print("微博创建人id", topic_detail['creator']['uid'])

            print(topic_detail['image']['url'])

                  # ,longText['url_objects'][0]['object']['timestamp'])
            # 超话信息
            # print(longText['url_objects'][0]['object']['object']['display_name'],longText['url_objects'][0]['object']['object']['summary'])

        # entry['duration']=tree.xpath('//*[@id="container"]/div[%s]/p[4]/text()'%i)
        # entry['start_time']=tree.xpath('//*[@id="container"]/div[%s]/p[2]/text()'%i)
        # entry['end_time'] =tree.xpath('//*[@id="container"]/div[%s]/p[3]/text()'%i)

    # entry['title'] = tree.xpath('//*[@id="container"]/div[50]/p[1]')

